<?php
/**
 * Created by PhpStorm.
 * User: liang
 * Email:1050187159@qq.com
 * Date: 2018/8/15
 * Time: 23:51
 */
namespace Admin\Model;

use Think\Model;

class InvestModel extends Model
{
    protected $trueTableName = 'invests';

    public function investsAdd()
    {
        $title = I('post.title');
        $english_title = strtoupper(I('post.english_title'));
        $is_show = I('post.is_show');
        $sort = I('post.sort', 0);
        $content = I('post.content');
        $image = I('post.image');
        $size = I('post.size');

        if (empty($title)) {
            $this->error = '标题不能为空';
            return false;
        }
        if (mb_strlen($title) > 30) {
            $this->error = '标题长度不能超过30';
            return false;
        }
        if (empty($english_title)) {
            $this->error = '英文标题不能为空';
            return false;
        }
        if (mb_strlen($english_title) > 50) {
            $this->error = '英文标题长度不能超过50';
            return false;
        }

        if (empty($size)) {
            $this->error = '图片大小必填';
            return false;
        }
        $size = explode('x',$size);
        if ((!is_integer((int)$size[0])) or (!is_integer((int)$size[1]))) {
            $this->error = '图片大小输入的格式错误';
            return false;
        }

        $Image = new \Think\Image();
        $Image->open(APP_PATH.$image);
        $mobile_image = '/thumb/'.date('Y-m-d', time()).'/';
        mkdir(APP_PATH.$mobile_image, 0777, true);
        $mobile_image = $mobile_image.basename($image);
        //按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
        $Image->thumb((int)$size[0], (int)$size[1])->save(APP_PATH.$mobile_image);

        if (empty($image)) {
            $this->error = '必须上传图片';
            return false;
        }
        if (is_integer($sort)) {
            $this->error = '排序必须为整数';
            return false;
        }
        if (empty($content)) {
            $this->error = '内容不能为空';
            return false;
        }

        $this->title = $title;
        $this->english_title = $english_title;
        $this->is_show = $is_show;
        $this->image = $image;
        $this->sort = $sort;
        $this->content = $content;
        $this->mobile_image = $mobile_image;
        $this->size = I('post.size');
        $this->pub_time = time();

        if ($this->add()) {
            return true;
        } else {
            return false;
        }

    }

    public function investsModify()
    {
        $id = I('post.id');
        $title = I('post.title');
        $english_title = strtoupper(I('post.english_title'));
        $is_show = I('post.is_show');
        $sort = I('post.sort', 0);
        $content = I('post.content');
        $image = I('post.image');
        $size = I('post.size');

        $invest = $this->where(array('id'=>array('eq',$id)))->find();

        if (empty($title)) {
            $this->error = '标题不能为空';
            return false;
        }
        if (mb_strlen($title) > 30) {
            $this->error = '标题长度不能超过30';
            return false;
        }
        if (empty($english_title)) {
            $this->error = '英文标题不能为空';
            return false;
        }
        if (mb_strlen($english_title) > 30) {
            $this->error = '英文标题长度不能超过30';
            return false;
        }

        if (empty($size)) {
            $this->error = '图片大小必填';
            return false;
        }
        $size = explode('x',$size);
        if ((!is_integer((int)$size[0])) or (!is_integer((int)$size[1]))) {
            $this->error = '图片大小输入的格式错误';
            return false;
        }

        if (empty($image)) {
            $this->error = '必须上传图片';
            return false;
        }

        $mobile_image = $invest['mobile_image'];
        if ($invest['image'] != $image) {
            //重新上传了图片,则需要删除原图片
            unlink(APP_PATH . $invest['image']);
            unlink(APP_PATH . $invest['mobile_image']);

            //更换了图片就需要压缩
            $Image = new \Think\Image();
            $Image->open(APP_PATH . $image);
            $mobile_image = '/thumb/' . date('Y-m-d', time()) . '/';
            mkdir(APP_PATH . $mobile_image, 0777, true);
            $mobile_image = $mobile_image . basename($image);
            //按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
            $Image->thumb((int)$size[0], (int)$size[1])->save(APP_PATH . $mobile_image);
        } else {
            if ($invest['size'] != I('post.size')) {
                //先删除原始的手机压缩图片
                unlink(APP_PATH . $invest['mobile_image']);
                //更换了图片就需要压缩
                $Image = new \Think\Image();
                $Image->open(APP_PATH . $image);
                $mobile_image = '/thumb/' . date('Y-m-d', time()) . '/';
                mkdir(APP_PATH . $mobile_image, 0777, true);
                $mobile_image = $mobile_image . basename($image);
                //按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
                $Image->thumb((int)$size[0], (int)$size[1])->save(APP_PATH . $mobile_image);
            }
        }

        if (is_integer($sort)) {
            $this->error = '排序必须为整数';
            return false;
        }
        if (empty($content)) {
            $this->error = '内容不能为空';
            return false;
        }

        $this->title = $title;
        $this->english_title = $english_title;
        $this->is_show = $is_show;
        $this->image = $image;
        $this->sort = $sort;
        $this->content = $content;
        $this->size = $size;
        $this->mobile_image = $mobile_image;

        $result = $this->where(array('id' => array('eq', $id)))->save();
        if ($result !== false) {
            return true;
        } else {
            return false;
        }
    }


}